package api

import (
	"encoding/json"
	"fmt"
	"main/dto"
	"main/utils"
	"time"
)

// 黄金数据 API 请求
/**
func FetchGoldData() interface{} {
	url := "https://free.xwteam.cn/api/gold/trade"
	headers := map[string]string{
		"Content-Type": "application/json",
	}
	var response, _ = utils.HttpPost(url, headers, nil, "", 10*time.Second)

	var responseMap map[string]interface{}
	var err = json.Unmarshal(response.ResponseBody, &responseMap)
	if err != nil {
		fmt.Println("解析响应失败:", err)
		return nil
	}

	// 从响应数据中提取 GJ 数组
	data, ok := responseMap["data"].(map[string]interface{})
	if !ok {
		fmt.Println("数据格式错误")
		return nil
	}

	// 获取 GJ 数组
	gjData, ok := data["GJ"].([]interface{})
	if !ok {
		fmt.Println("GJ 数据未找到")
		return nil
	}

	// 查找伦敦金
	for _, item := range gjData {
		gjItem, ok := item.(map[string]interface{})
		if !ok {
			continue
		}

		symbol, ok := gjItem["Symbol"].(string)
		if ok && symbol == "GJ_Au" {
			// 返回 GJ_Au 对应的数据
			return gjItem
		}
	}

	fmt.Println("伦敦金未找到")
	return nil
}
*/

func FetchGoldData() dto.FinanceData {
	url := "https://push2.eastmoney.com/api/qt/stock/get?invt=2&fltt=1&fields=f58%2Cf734%2Cf107%2Cf57%2Cf43%2Cf59%2Cf169%2Cf170%2Cf152%2Cf177%2Cf111%2Cf46%2Cf60%2Cf44%2Cf45%2Cf86%2Cf47%2Cf19%2Cf532%2Cf161%2Cf171%2Cf48%2Cf39%2Cf49%2Cf50&secid=122.XAU&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&dect=1&_=1744694318756"
	// 设置请求头
	headers := map[string]string{
		"Accept":             "*/*",
		"Accept-Language":    "zh-CN,zh;q=0.9",
		"Connection":         "keep-alive",
		"Referer":            "https://quote.eastmoney.com/q/122.XAU.html",
		"Sec-Fetch-Dest":     "script",
		"Sec-Fetch-Mode":     "no-cors",
		"Sec-Fetch-Site":     "same-site",
		"User-Agent":         "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
		"sec-ch-ua":          "\"Not(A:Brand\";v=\"99\", \"Google Chrome\";v=\"133\", \"Chromium\";v=\"133\"",
		"sec-ch-ua-mobile":   "?0",
		"sec-ch-ua-platform": "\"Windows\"",
		"Cookie":             "qgqp_b_id=f77b18c60dfc8899b90ad89ed3b13279; fullscreengg=1; fullscreengg2=1; st_si=36420492508065; emshistory=%5B%22%E8%8B%B1%E5%9B%BD%E5%AF%8C%E6%97%B6100%22%2C%22%E6%B8%AF%E4%BC%A6%E6%95%A6%E9%87%91%22%5D; websitepoptg_api_time=1744688787022; HAList=ty-2-950180-%u79D1%u521BAI%2Cty-171-US10Y-%u7F8E%u56FD10%u5E74%u671F%u56FD%u503A%2Cty-1-000905-%u4E2D%u8BC1500%2Cty-107-YINN-%u4E09%u500D%u505A%u591A%u5BCC%u65F6%u4E2D%u56FDETF-Direxion%2Cty-122-XAU-%u9EC4%u91D1/%u7F8E%u5143%2Cty-100-FTSE-%u82F1%u56FD%u5BCC%u65F6100%2Cty-90-BK0547-%u9EC4%u91D1%u6982%u5FF5%2Cty-155-0J39-%u54C8%u83AB%u5C3C%u9EC4%u91D1%2Cty-2-931238-SSH%u9EC4%u91D1%u80A1%u7968%2Cty-100-NDX-%u7EB3%u65AF%u8FBE%u514B",
	}

	var response, _ = utils.HttpPost(url, headers, nil, "", 10*time.Second)

	// 解析响应
	var responseMap map[string]interface{}
	var err = json.Unmarshal(response.ResponseBody, &responseMap)
	if err != nil {
		fmt.Println("解析响应失败:", err)
		return dto.FinanceData{}
	}

	/// 提取数据
	data, ok := responseMap["data"].(map[string]interface{})
	if !ok {
		fmt.Println("数据格式错误")
		return dto.FinanceData{}
	}

	// 返回黄金数据
	return dto.FinanceData{
		LatestPrice:    fmt.Sprintf("%.2f", data["f43"].(float64)/100),    // 最新价 (f43)
		HighestPrice:   fmt.Sprintf("%.2f", data["f44"].(float64)/100),    // 最高价 (f44)
		LowestPrice:    fmt.Sprintf("%.2f", data["f45"].(float64)/100),    // 最低价 (f45)
		OpenPrice:      fmt.Sprintf("%.2f", data["f46"].(float64)/100),    // 开盘价 (f46)
		LastClosePrice: fmt.Sprintf("%.2f", data["f60"].(float64)/100),    // 昨日收价格 (f60)
		PricePercent:   fmt.Sprintf("%.2f%%", data["f170"].(float64)/100), // 涨跌幅 (%) (f170)
	}
}
